Imports System.Reflection
Imports System.Collections.Generic
Imports System.Linq
Imports Microsoft.Xna.Framework
Imports Microsoft.Xna.Framework.Audio
Imports Microsoft.Xna.Framework.Content
Imports Microsoft.Xna.Framework.GamerServices
Imports Microsoft.Xna.Framework.Graphics
Imports Microsoft.Xna.Framework.Input
Imports Microsoft.Xna.Framework.Media
Imports Microsoft.Xna.Framework.Input.Touch
Imports System.IO

Public Class Splash
	Inherits Sprite
	Private _player As Boolean = False

	''' <summary>
	''' Constructors a new player.
	''' </summary>
	Public Sub New(room As RoomNew, position As Vector2, GraphicsDevice__1 As GraphicsDevice, spriteEffect As SpriteEffects, player As Boolean)
		_player = player
		graphicsDevice = GraphicsDevice__1
		spriteRoom = room
		LoadContent()

		'TAKE PLAYER Position



		Reset(position, spriteEffect)
	End Sub

	''' <summary>
	''' Resets the player to life.
	''' </summary>
	''' <param name="position">The position to come to life at.</param>
	Public Sub Reset(position As Vector2, spriteEffect As SpriteEffects)
		_position = New Position(New Vector2(graphicsDevice.Viewport.Width, graphicsDevice.Viewport.Height), New Vector2(Player.SPRITE_SIZE_X, Player.SPRITE_SIZE_Y))
		_position.X = position.X + Player.PLAYER_L_PENETRATION
		_position.Y = position.Y + Player.PLAYER_STAND_FLOOR_PEN
		Velocity = Vector2.Zero
		Energy = 0

		flip = spriteEffect

		spriteState.Clear()

		Show()


	End Sub

	Public Sub Show()
		Show(Enumeration.PriorityState.Normal, Nothing)
	End Sub

	Public Sub Show(stoppable As Boolean)
		Show(Enumeration.PriorityState.Normal, stoppable)
	End Sub

	Public Sub Show(priority As Enumeration.PriorityState)
		Show(priority, Nothing)
	End Sub

	Public Sub Show(priority As Enumeration.PriorityState, stoppable As System.Nullable(Of Boolean))
		If priority = Enumeration.PriorityState.Normal And sprite.IsStoppable = stoppable Then
			Return
		End If

		If _player = True Then
			spriteState.Add(Enumeration.State.splash_player, priority)
		Else
			spriteState.Add(Enumeration.State.splash_enemy, priority)
		End If

		sprite.PlayAnimation(spriteSequence, spriteState.Value())
	End Sub

	''' Loads the player sprite sheet and sounds.
	''' </summary>
	''' <note>i will add a parameter read form app.config</note>
	''' 
	''' 
	Private Sub LoadContent()

		spriteSequence = New List(Of Sequence)()
		Dim ax As New System.Xml.Serialization.XmlSerializer(spriteSequence.[GetType]())

		Dim txtReader As Stream = Microsoft.Xna.Framework.TitleContainer.OpenStream(PrinceOfPersiaGame.CONFIG_PATH_CONTENT & PrinceOfPersiaGame.CONFIG_PATH_SEQUENCES & Me.[GetType]().Name.ToString().ToUpper() & "_sequence.xml")


		'TextReader txtReader = File.OpenText(PrinceOfPersiaGame.CONFIG_PATH_CONTENT + PrinceOfPersiaGame.CONFIG_PATH_SEQUENCES + "KID_sequence.xml");
		'Stream astream = this.GetType().Assembly.GetManifestResourceStream("PrinceOfPersia.resources.KID_sequence.xml");
		spriteSequence = DirectCast(ax.Deserialize(txtReader), List(Of Sequence))

		For Each s As Sequence In spriteSequence
			s.Initialize(SpriteRoom.content)
		Next

		' Calculate bounds within texture size.         
		'faccio un rettangolo che sia largo la metà del frame e che parta dal centro
		Dim top As Integer = 0
		'StandAnimation.FrameHeight - height - 128;
		Dim left As Integer = 0
		'PLAYER_L_PENETRATION; //THE LEFT BORDER!!!! 19
		Dim width As Integer = 114
		'(int)(StandAnimation.FrameWidth);  //lo divido per trovare punto centrale *2)
		Dim height As Integer = 114
		'(int)(StandAnimation.FrameHeight);

		localBounds = New Rectangle(left, top, width, height)

		' Load sounds.            
		'killedSound = _room.content.Load<SoundEffect>("Sounds/PlayerKilled");
		'jumpSound = _room.content.Load<SoundEffect>("Sounds/PlayerJump");
		'fallSound = _room.content.Load<SoundEffect>("Sounds/PlayerFall");
	End Sub

	''' <summary>
	''' Handles input, performs physics, and animates the player sprite.
	''' </summary>
	''' <remarks>
	''' We pass in all of the input states so that our game is only polling the hardware
	''' once per frame. We also pass the game's orientation because when using the accelerometer,
	''' we need to reverse our motion when the orientation is in the LandscapeRight orientation.
	''' </remarks>
	Public Sub Update(gameTime As GameTime, keyboardState As KeyboardState, gamePadState As GamePadState, touchState As TouchCollection, accelState As AccelerometerState, orientation As DisplayOrientation)


		Dim elapsed As Single = CSng(gameTime.ElapsedGameTime.TotalSeconds)
		' TODO: Add your game logic here.
		sprite.UpdateFrame(elapsed, _position, flip, spriteState)

	End Sub

	''' <summary>
	''' Draws the animated player.
	''' </summary>
	Public Sub Draw(gameTime As GameTime, spriteBatch As SpriteBatch)
		sprite.DrawSprite(gameTime, spriteBatch, _position.Value, flip, 0.5F)
	End Sub




End Class
